var path;
var cur;

$(document).ready(function(){

	// init
	init();
	
	// edit address
	$(".editAddress").live("click",editAddress);
	
	// update address
	$(".submitAddress").live("click",updateAddress);
	
	// delete address
	$(".deleteAddress").live("click",deleteAddress);
	
	// set default address
	$(".setDefault").live("click",setDefaultAddress);
	
	// skip add address
	//(".addNewAddressBtn").live("click",skipAddAddress);
	
});

function init(){
	path = $("#path").val();
}

function skipAddAddress(){
	window.location.href=path+"/user/address/edit/0";
}

function editAddress(){
	cur = $(this);
	var addressId = getAddressId();
	
	if(isEmpty(addressId)){
		return;
	}
	
	var url = path + "/user/address/edit/" + addressId;
	window.location.href = url;	
}

function updateAddress(){
	if(!checkInput()){
		return;
	}
	
	$("body").find("form[name='address-form']").submit();
}

function deleteAddress(){
	
	cur = $(this);
	var addressId = getAddressId();
	
	if(isEmpty(addressId)){
		return;
	}
	
	$.dialog({
        content : '确认删除吗？',
        title : 'ok',
        ok : function() {
            var url = path + "/user/address/remove/"+addressId;
            $.ajax({
                type: "post",
                dataType: "text",
                async:true,
                url:url,
                success: function (result) {
                	if(result != "YYK-10000"){
                		$.dialog({
                			content : '系统繁忙，请稍候重试',
                			title : '温馨提示',
                			lock : true,
                			time : 2000
                		});
                	}else{
                		$.dialog({
                			content : '删除成功',
                			title : '温馨提示',
                			lock : true,
                			time : 1000
                		});
                		$(".contentWrap").load(path+"/user/address/find #wrapper");
                	}
                }
        	 });
            
        },
        cancel : function() {
        },
        lock : true
    });
}

function setDefaultAddress(){
	cur = $(this);
	var addressId = getAddressId();
	
	if(isEmpty(addressId)){
		return;
	}
	
	var url = path + "/user/address/default/"+addressId;
	
	$.ajax({
        type: "post",
        dataType: "text",
        async:true,
        url:url,
        success: function (result) {
        	if(result != "YYK-10000"){
        		$.dialog({
        			content : '系统繁忙，请稍候重试',
        			title : '温馨提示',
        			lock : true,
        			time : 2000
        		});
        	}else{
        		$.dialog({
        			content : '设置默认地址成功',
        			title : '温馨提示',
        			lock : true,
        			time : 1000
        		});
        		$(".contentWrap").load(path+"/user/address/find #wrapper");
        	}
        }
	 });
}

function getAddressId(){
	return cur.parents(".recepient").attr("id");
}

function checkInput(){
	
	var addressForm = $("body").find("form[name='address-form']");
	// 收货人姓名
	var recepientName = addressForm.find("input[name='recepientName']").val();
	if(isEmpty(recepientName)){
		$.dialog({
			content : '请填写收货人姓名',
			title : '温馨提示',
			lock : true,
			time : 1500
		});
		return false;
	}
	
	// 收货人电话
	var recepientTel = addressForm.find("input[name='recepientTel']").val();
	if(isEmpty(recepientTel)){
		$.dialog({
			content : '请填写手机号码',
			title : '温馨提示',
			lock : true,
			time : 1500
		});
		return false;
	}
	if(!check_mobile(recepientTel)){
		$.dialog({
			content : '手机号码格式不对',
			title : '温馨提示',
			lock : true,
			time : 1500
		});
		return false;
	}
	
	// 收货人省市区
	var recepientProvinceId = addressForm.find("input[name='recepientProvinceId']").val();
	var recepientCityId = addressForm.find("input[name='recepientCityId']").val();
	var recepientAreaId = addressForm.find("input[name='recepientAreaId']").val();
	if(isEmpty(recepientProvinceId) || isEmpty(recepientCityId) || isEmpty(recepientAreaId)){
		$.dialog({
			content : '请选择收货地址',
			title : '温馨提示',
			lock : true,
			time : 1500
		});
		return false;
	}
	
	// 收货人街道
	var recepientTel = addressForm.find("input[name='recepientAddress']").val();
	if(isEmpty(recepientTel)){
		$.dialog({
			content : '请填写街道',
			title : '温馨提示',
			lock : true,
			time : 1500
		});
		return false;
	}
	
	return true;
}
